﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ArivisTests.Models;

namespace ArivisTests.Controllers
{
    [Authorize]
    public class TestPlanController : Controller
    {
        private ArivisTestsDBContext db = new ArivisTestsDBContext();

        //
        // GET: /TestPlan/

        public ActionResult Index()
        {
            return View(db.TestPlans.ToList());
        }

        //
        // GET: /TestPlan/Details/5

        public ActionResult Details(int id = 0)
        {
            TestPlan testplan = db.TestPlans.Find(id);
            if (testplan == null)
            {
                return HttpNotFound();
            }
            return View(testplan);
        }

        //
        // GET: /TestPlan/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /TestPlan/Create

        [HttpPost]
        public ActionResult Create(TestPlan testplan, int[] scenarios)
        {
            if (ModelState.IsValid)
            {
                var dbTestPlan = db.TestPlans.Create();
                db.TestPlans.Add(dbTestPlan);

                dbTestPlan.Name = testplan.Name;
                if (scenarios != null)
                {
                    dbTestPlan.Tests = db.TestScenarios.Where(x => scenarios.Contains(x.Id)).ToList();
                }

                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(testplan);
        }

        //
        // GET: /TestPlan/Edit/5

        public ActionResult Edit(int id = 0)
        {
            TestPlan testplan = db.TestPlans.Find(id);
            if (testplan == null)
            {
                return HttpNotFound();
            }
            return View(testplan);
        }

        //
        // POST: /TestPlan/Edit/5

        [HttpPost]
        public ActionResult Edit(TestPlan testplan, int[] scenarios)
        {
            if (ModelState.IsValid)
            {
                var dbTestPlan = db.TestPlans.First(x => x.Id == testplan.Id);
                dbTestPlan.Name = testplan.Name;

                if (scenarios == null)
                {
                    dbTestPlan.Tests.Clear();
                }
                else
                {
                    var dbScenarios = db.TestScenarios.Where(x => scenarios.Contains(x.Id)).ToList();
                    var toRemove = dbTestPlan.Tests.Except(dbScenarios).ToList();
                    foreach (var x in toRemove) dbTestPlan.Tests.Remove(x);
                    var toAdd = dbScenarios.Except(dbTestPlan.Tests).ToList();
                    foreach (var x in toAdd) dbTestPlan.Tests.Add(x);
                }
                
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(testplan);
        }

        //
        // GET: /TestPlan/Delete/5

        public ActionResult Delete(int id = 0)
        {
            TestPlan testplan = db.TestPlans.Find(id);
            if (testplan == null)
            {
                return HttpNotFound();
            }
            return View(testplan);
        }

        //
        // POST: /TestPlan/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            TestPlan testplan = db.TestPlans.Find(id);
            db.TestPlans.Remove(testplan);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}